﻿Imports Co9320.NamaAlert.Model

Namespace Extension

    Public NotInheritable Class PluginSettings
        Implements IPluginSettings, ISerializable


        Private Table As New Dictionary(Of String, Object)

#Region "Properties"

        Default Public Property Item(ByVal name As String) As String Implements IPluginSettings.Item
            Get
                If Table.ContainsKey(name) Then
                    Return CStr(Table(name))
                Else
                    Return Nothing
                End If
            End Get
            Set(ByVal value As String)
                If Table.ContainsKey(name) Then
                    Table(name) = value
                Else
                    Table.Add(name, value)
                End If
            End Set
        End Property

        Public ReadOnly Property Names() As System.Collections.Generic.IList(Of String) Implements IPluginSettings.Names
            Get
                Return Table.Keys.ToArray
            End Get
        End Property

#End Region

        Public Function Contains(ByVal name As String) As Boolean Implements IPluginSettings.Contains
            Return Table.ContainsKey(name)
        End Function

        Public Sub SetInteger(ByVal name As String, ByVal value As Integer) Implements IPluginSettings.SetInteger
            If Table.ContainsKey(name) Then
                Table(name) = value
            Else
                Table.Add(name, value)
            End If
        End Sub

        Public Function GetInteger(ByVal name As String) As Integer? Implements IPluginSettings.GetInteger
            If Not Table.ContainsKey(name) Then
                Return Nothing
            End If
            Dim value As Integer
            If Integer.TryParse(CStr(Table(name)), value) Then
                Return value
            Else
                Return Nothing
            End If
        End Function

        Public Function GetInteger(ByVal name As String, ByVal defaultValue As Integer) As Integer Implements IPluginSettings.GetInteger
            If Not Table.ContainsKey(name) Then
                Return defaultValue
            End If
            Dim value As Integer
            If Integer.TryParse(CStr(Table(name)), value) Then
                Return value
            Else
                Return defaultValue
            End If
        End Function


        Public Sub SetString(ByVal name As String, ByVal value As String) Implements IPluginSettings.SetString
            If Table.ContainsKey(name) Then
                Table(name) = value
            Else
                Table.Add(name, value)
            End If
        End Sub

        Public Function GetString(ByVal name As String) As String Implements IPluginSettings.GetString
            If Not Table.ContainsKey(name) Then
                Return Nothing
            End If
            Return CStr(Table(name))
        End Function

        Public Function GetString(ByVal name As String, ByVal defaultValue As String) As String Implements IPluginSettings.GetString
            If Not Table.ContainsKey(name) Then
                Return defaultValue
            End If
            Return CStr(Table(name))
        End Function

        Public Function GetStringWithSimpleEncryption(ByVal name As String) As String Implements IPluginSettings.GetStringWithSimpleEncryption

        End Function

        Public Function GetStringWithSimpleEncryption(ByVal name As String, ByVal defaultValue As String) As String Implements IPluginSettings.GetStringWithSimpleEncryption

        End Function

        Public Sub SetStringWithSimpleEncryption(ByVal name As String, ByVal value As String) Implements IPluginSettings.SetStringWithSimpleEncryption

        End Sub

        Public Sub SetBoolean(ByVal name As String, ByVal value As Boolean) Implements IPluginSettings.SetBoolean
            If Table.ContainsKey(name) Then
                Table(name) = value
            Else
                Table.Add(name, value)
            End If
        End Sub

        Public Function GetBoolean(ByVal name As String) As Boolean? Implements IPluginSettings.GetBoolean
            If Not Table.ContainsKey(name) Then
                Return Nothing
            End If
            Dim value As Boolean
            If Boolean.TryParse(CStr(Table(name)), value) Then
                Return value
            Else
                Return Nothing
            End If
        End Function

        Public Function GetBoolean(ByVal name As String, ByVal defaultValue As Boolean) As Boolean Implements IPluginSettings.GetBoolean
            If Not Table.ContainsKey(name) Then
                Return Nothing
            End If
            Dim value As Boolean
            If Boolean.TryParse(CStr(Table(name)), value) Then
                Return value
            Else
                Return defaultValue
            End If
        End Function

        Public Function Remove(ByVal name As String) As Boolean Implements IPluginSettings.Remove
            Return Table.Remove(name)
        End Function

        Public Sub Clear() Implements IPluginSettings.Clear
            Table.Clear()
        End Sub

#Region "ISerializable"

        Public Sub Deserialize(ByVal rootElement As System.Xml.Linq.XElement) Implements Model.ISerializable.Deserialize
            Me.Clear()
            For Each e In rootElement.Elements
                Me(e.Name.LocalName) = e.Value
            Next
        End Sub

        Public Function Serialize() As System.Xml.Linq.XElement Implements Model.ISerializable.Serialize
            Return Serialize(Me.GetType.Name)
        End Function

        Public Function Serialize(ByVal name As String) As System.Xml.Linq.XElement Implements Model.ISerializable.Serialize

            Dim element = New XElement(name)
            For Each name In Names
                element.Add(<<%= name %>><%= Me(name) %></>)
            Next
            Return element

        End Function

#End Region

 
    End Class

End Namespace
